package photosalvage.util;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.activation.MimetypesFileTypeMap;
import javax.mail.internet.MimeUtility;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetWrapperFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		try {
			URI uri = new URI(request.getRequestURI());
			String path = uri.getPath();
			int slash = path.lastIndexOf('/');
			String fileName = path.substring(slash + 1);
			String contentType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(fileName);

			chain.doFilter(req, new MyHttpServletResponseWrapper((HttpServletResponse) resp, contentType));
		} catch (URISyntaxException e) {
			throw new IOException(e);
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
	}

}
